﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TPCN.DAL;
using Telerik.Web.UI;

namespace TPCN
{
    public partial class Giohang : System.Web.UI.Page
    {
        private static ShoppingCartObj cart = new ShoppingCartObj();
        private static string HangID;

        private const string CMD_DELETE = "cmdDelete";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadData();
                BindGrid();
            }
        }

        protected void LoadData()
        {
            if (HttpContext.Current.Session["cart"] != null)
            {
                cart = (ShoppingCartObj)HttpContext.Current.Session["cart"];
            }
            else
                cart = null;
        }

        protected void BindGrid()
        {
            if (cart != null)
            {
                gridCart.DataSource = cart.Listproduct;
                gridCart.DataBind();
                if (!String.IsNullOrEmpty(cart.Tongtien))
                {
                    lblTongtien.Text = cart.Tongtien;
                }
            }
            else
            {
                gridCart.DataSource = null;
                gridCart.DataBind();
                lblTongtien.Text = "0";
            }
        }

        protected void btnHome_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Session["cart"] = cart;
            Response.Redirect("~");
        }

        protected void gridCart_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            HangID = e.CommandArgument.ToString();
            if (e.CommandName == CMD_DELETE)
            {
                cart.Delete(HangID);
                BindGrid();
                Response.Redirect(Request.RawUrl);
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gridCart.Rows.Count; i++)
            {
                GridViewRow row = gridCart.Rows[i];
                if (row.RowType == DataControlRowType.DataRow)
                {
                    RadNumericTextBox soluong = (RadNumericTextBox)row.FindControl("txtSoluong");
                    if (!String.IsNullOrEmpty(soluong.Text))
                        cart.Update(row.Cells[1].Text, (int)soluong.Value);
                }
            }
            BindGrid();
            Response.Redirect(Request.RawUrl);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gridCart.Rows.Count; i++)
            {
                GridViewRow row = gridCart.Rows[i];
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chk = (CheckBox)row.FindControl("chkSelect");
                    if (chk.Checked)
                    {
                        cart.Delete(row.Cells[1].Text);
                    }
                }
            }
            HttpContext.Current.Session["cart"] = cart;
            BindGrid();
            Response.Redirect(Request.RawUrl);
        }

        protected bool isLogin()
        {
            return (HttpContext.Current.Session["user"] != null);
        }

        protected void btnDathang_Click(object sender, EventArgs e)
        {
            if (!isLogin())
            {
                Response.Redirect("~/Accounts/Logon.aspx?returnurl=~/Thanhtoan.aspx");
            }
            else
            {
                Response.Redirect("~/Thanhtoan.aspx");
            }
        }
    }
}